//
//  PopoverController.m
//  TEST
//
//  Created by beisheng03 on 2017/9/25.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "PopoverController.h"
#import "GoodsAttributeCell.h"
#import "GoodsAttributeHeader.h"
#import "GoodsAttributeModel.h"
#import "GoodsAttrSetionModel.h"
#import "GoodsSpecListModel.h"
#import "GoodsSpecSubModel.h"
@interface PopoverController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *bottomBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomBtnH;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIImageView *goodsImageView;
@property (weak, nonatomic) IBOutlet UIView *goodsImgBg;
@property (weak, nonatomic) IBOutlet UILabel *attrbuteDeLabel;


@end

@implementation PopoverController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSubViews];
    
    self.myCollectionView.delegate = self;
    
    self.myCollectionView.dataSource = self;
    

    
    
    [self.myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GoodsAttributeCell class]) bundle:nil] forCellWithReuseIdentifier:[GoodsAttributeCell identifierStr]];
    
    //注册分区头标题
    [self.myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GoodsAttributeHeader class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[GoodsAttributeHeader identifierStr]];
    
    
    [self.myCollectionView registerClass:[UICollectionReusableView class]
              forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                     withReuseIdentifier:@"CollectionViewFooterView"];

    
    self.view.backgroundColor = [UIColor clearColor];
    
    if (IPhoneX) {
        self.bottomBtnH.constant = 34;
    } else {
        self.bottomBtnH.constant = 0;

    }
    
    
}



- (void)setupSubViews{
    
    self.priceLabel.font = MYFONT(16);
    
    self.goodsImageView.layer.cornerRadius = 4;
    self.goodsImageView.layer.masksToBounds = YES;
    self.goodsImgBg.layer.cornerRadius = 5;
    self.goodsImgBg.layer.masksToBounds = YES;
    
    self.goodsImgBg.backgroundColor = viewBgGrey;
    
    self.attrbuteDeLabel.font = MYFONT(14);
    
    if (self.model.img_list.count > 0) {
        
        [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:self.model.img_list.firstObject] placeholderImage:nil];

    }
    
    if ([self.model.tag isEqualToString:@"0"]) {
        
        self.model.tag = @"1";
        NSMutableString *strM = [NSMutableString string];
        for (GoodsSpecListModel *temp in self.model.spec_list) {
            
            [strM appendString:temp.spec_name];
            
            for (GoodsSpecSubModel *sub in temp.value) {
                sub.isSelect = @"0";
            }
            
            temp.value.firstObject.isSelect = @"1";
            
        }
        
        self.priceLabel.text = self.model.selectSkuModel.price;
        
        if (strM.length > 0) {
            self.attrbuteDeLabel.text = [NSString stringWithFormat:@"请选择%@",strM];
        } else {
            self.attrbuteDeLabel.text = @"默认";
        }
    }
    
   
    
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.model.spec_list.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.model.spec_list[section].value.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    GoodsAttributeCell *temp = (GoodsAttributeCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[GoodsAttributeCell identifierStr] forIndexPath:indexPath];
    
    temp.model = self.model.spec_list[indexPath.section].value[indexPath.item];
    
    
    return temp;
    
   
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        
        GoodsAttributeHeader *view = (GoodsAttributeHeader *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                            withReuseIdentifier:[GoodsAttributeHeader identifierStr]
                                                                                   forIndexPath:indexPath];
        
        view.str = self.model.spec_list[indexPath.section].spec_name;
        
        return view;
        
    } else {
        
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                            withReuseIdentifier:@"CollectionViewFooterView"
                                                                                   forIndexPath:indexPath];
        
        return view;
        
    }
    
    
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KScreenW, 44);
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
 
    
//    if (indexPath.item == 0) {
//
//        return CGSizeMake(KScreenW,44);
//
//    } else {
        NSString  *textStr = self.model.spec_list[indexPath.section].value[indexPath.item].spec_value_name;
        
        CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(14), NSFontAttributeName, nil]];
        
        return CGSizeMake(size.width + 12,22);
//    }
    
    
   
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsSpecListModel *temp = self.model.spec_list[indexPath.section];
    
    for (GoodsSpecSubModel *sub in temp.value) {
        sub.isSelect = @"0";
    }
    
    GoodsSpecSubModel *select = self.model.spec_list[indexPath.section].value[indexPath.item];
    
    select.isSelect = @"1";
    
    [collectionView reloadData];

    
    
    
    NSMutableArray *arrayM = [NSMutableArray array];
    NSMutableString *displayStr = [NSMutableString  string];
    for (GoodsSpecListModel *temp in self.model.spec_list) {
        NSMutableString *strM = [NSMutableString string];
        for (GoodsSpecSubModel *sub in temp.value) {
            
            if ([sub.isSelect isEqualToString:@"1"]) {
                
                [displayStr appendString:sub.spec_value_name];
                
                [displayStr appendString:@","];
                
                [strM appendString:sub.spec_id];
                
                [strM appendString:@":"];

                [strM appendString:sub.spec_value_id];
                
                [arrayM addObject:strM];
            }
        }
    }
    
    NSString *lastStr =  [displayStr substringToIndex:displayStr.length - 1];
    self.model.displayStr = lastStr;
    
    
    
    
    NSInteger i = 0;
    NSInteger j = arrayM.count;
    for (GoodsSkuListModel *skuModel in self.model.sku_list) {
        i = 0;
        for (NSString *str1 in arrayM) {
            
            if ([skuModel.attr_value_items_format containsString:str1]) {
                i++;
            }
        }
        
        if (i == j) {
            self.model.selectSkuModel = skuModel;
        }
       
    }
    
    self.priceLabel.text = self.model.selectSkuModel.price;
    
    
    
    NSLog(@"%@",self.model.selectSkuModel.sku_id);

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sureBtnDidClick:(UIButton *)sender {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DidSelectGoodsSpec" object:nil];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
